package com.ncmmall.mall.controller.business.distribution;

import com.ncmmall.application.business.distribution.DistributorLevelApplication;
import com.ncmmall.domain.business.distribution.DistributorLevel;
import com.ncmmall.domain.business.distribution.QDistributorLevel;
import com.ncmmall.domain.business.goods.Goods;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 分销商等级管理
 *
 * @author likaikai
 * @date 2020-8-20 19:57:13
 */
@Controller
@RequestMapping("/distributorLevel")
@Slf4j
public class DistributorLevelController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private DistributorLevelApplication distributorLevelApplication;

    /**
     * 分销商等级列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {

        String sql = "select * from distributor_level where level = :level";
        DistributorLevel level1 = queryChannelService.queryOneEntity(sql, ImmutableMap.of("level", 1), DistributorLevel.class);
        DistributorLevel level2 = queryChannelService.queryOneEntity(sql, ImmutableMap.of("level", 2), DistributorLevel.class);
        DistributorLevel level3 = queryChannelService.queryOneEntity(sql, ImmutableMap.of("level", 3), DistributorLevel.class);
        DistributorLevel level4 = queryChannelService.queryOneEntity(sql, ImmutableMap.of("level", 4), DistributorLevel.class);
        DistributorLevel level5 = queryChannelService.queryOneEntity(sql, ImmutableMap.of("level", 5), DistributorLevel.class);

        List<DistributorLevel> list = new ArrayList<>();
        list.add(level1 == null ? new DistributorLevel(DistributorLevel.Level.ONE) : level1);
        list.add(level2 == null ? new DistributorLevel(DistributorLevel.Level.TWO) : level2);
        list.add(level3 == null ? new DistributorLevel(DistributorLevel.Level.THREE) : level3);
        list.add(level4 == null ? new DistributorLevel(DistributorLevel.Level.FOUR) : level4);
        list.add(level5 == null ? new DistributorLevel(DistributorLevel.Level.FIVE) : level5);

        model.addAttribute("list", list);

        return "mall/distribution/level/list";
    }


    /**
     * 编辑分销商等级
     */
    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(int level, Model model) {
        DistributorLevel distributorLevel = queryChannelService.findOne(QDistributorLevel.distributorLevel.level.eq(level), DistributorLevel.class);
        if (distributorLevel == null) {
            distributorLevel = new DistributorLevel();
            distributorLevel.setLevel(level);
        }

        if (distributorLevel.getApplyType() != null && distributorLevel.getApplyType().equals(2)) {
            List<Goods> goodsList = queryChannelService.findAll("select g.goods from DistributorLevelApplyGoods g where g.distributorLevel.level = :level", ImmutableMap.of("level", level), Goods.class);
            model.addAttribute("goodsList", goodsList);
        } else {
            model.addAttribute("goodsList", new ArrayList<>());
        }
        model.addAttribute("level", distributorLevel);

        return "mall/distribution/level/form";
    }


    /**
     * 编辑分销商等级
     */
    @RequestMapping(value = "/modify", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> modify(DistributorLevel params) {
        try {
            distributorLevelApplication.modify(params);
            return ImmutableMap.of("status", true, "message", "编辑成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            return ImmutableMap.of("status", false, "message", "编辑分销商等级异常");
        }
    }

    /**
     * 编辑分销商等级
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> remove(Long id) {
        try {
            distributorLevelApplication.remove(id);
            return ImmutableMap.of("status", true, "message", "删除成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            return ImmutableMap.of("status", false, "message", "分销商等级被使用,无法删除");
        }
    }



}
